\chapter{分支}

\section{逻辑运算符}

\subsection{关系运算符}

编程中经常需要使用关系运算符来比较两个数据的大小，比较的结果是一个布尔值（boolean），即True（非0）或False（0）。\\

在编程中需要注意，一个等号=表示赋值运算，而两个等号==表示比较运算。\\

\begin{table}[H]
	\centering
	\setlength{\tabcolsep}{5mm}{
		\begin{tabular}{|c|c|}
			\hline
			\textbf{数学符号} & \textbf{关系运算符} \\
			\hline
			$ < $             & <                   \\
			\hline
			$ > $             & >                   \\
			\hline
			$ \le $           & <=                  \\
			\hline
			$ \ge $           & >=                  \\
			\hline
			$ = $             & ==                  \\
			\hline
			$ \ne $           & !=                  \\
			\hline
		\end{tabular}
	}
\end{table}

\vspace{0.5cm}

\subsection{逻辑运算符}

逻辑运算符用于连接多个关系表达式，其结果也是一个布尔值。\\

\begin{enumerate}
	\item 逻辑与\&\&：当多个条件全部为True，结果为True。\\
	      \begin{table}[H]
		      \centering
		      \setlength{\tabcolsep}{5mm}{
			      \begin{tabular}{|c|c|c|}
				      \hline
				      \textbf{条件1} & \textbf{条件2} & \textbf{条件1 \&\& 条件2} \\
				      \hline
				      T              & T              & T                         \\
				      \hline
				      T              & F              & F                         \\
				      \hline
				      F              & T              & F                         \\
				      \hline
				      F              & F              & F                         \\
				      \hline
			      \end{tabular}
		      }
	      \end{table}

	\item 逻辑或||：多个条件至少有一个为True时，结果为True。\\
	      \begin{table}[H]
		      \centering
		      \setlength{\tabcolsep}{5mm}{
			      \begin{tabular}{|c|c|c|}
				      \hline
				      \textbf{条件1} & \textbf{条件2} & \textbf{条件1 || 条件2} \\
				      \hline
				      T              & T              & T                       \\
				      \hline
				      T              & F              & T                       \\
				      \hline
				      F              & T              & T                       \\
				      \hline
				      F              & F              & F                       \\
				      \hline
			      \end{tabular}
		      }
	      \end{table}

	\item 逻辑非!：条件为True时，结果为False；条件为False时，结果为True。\\
	      \begin{table}[H]
		      \centering
		      \setlength{\tabcolsep}{5mm}{
			      \begin{tabular}{|c|c|}
				      \hline
				      \textbf{条件} & \textbf{!条件} \\
				      \hline
				      T             & F              \\
				      \hline
				      F             & T              \\
				      \hline
			      \end{tabular}
		      }
	      \end{table}
\end{enumerate}

\newpage

\section{if}

\subsection{if}

if语句用于判断一个条件是否成立，如果成立则进入语句块，否则不执行。\\

\mybox{年龄}

\begin{lstlisting}[language=Java]
import java.util.Scanner;

public class Age {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.print("Enter your age: ");
		int age = scanner.nextInt();

		if (age > 0 && age < 18) {
			System.out.println("Minor");
		}

		scanner.close();
	}
}
\end{lstlisting}

\begin{tcolorbox}
	\mybox{运行结果}
	\begin{verbatim}
Enter your age: 17
Minor
\end{verbatim}
\end{tcolorbox}

\vspace{0.5cm}

\subsection{if-else}

if-else的结构与if类似，只是在if语句块中的条件不成立时，执行else语句块中的语句。\\

\mybox{闰年}

\begin{lstlisting}[language=Java]
import java.util.Scanner;

public class Leap {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.print("Enter a year: ");
		int year = scanner.nextInt();

		/*
			* A year is a leap year if it is
			* 1. exactly divisible by 4, and not divisible by 100;
			* 2. or is exactly divisible by 400
			*/
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
			System.out.println("Leap year");
		} else {
			System.out.println("Common year");
		}

		scanner.close();
	}
}
\end{lstlisting}

\begin{tcolorbox}
	\mybox{运行结果}
	\begin{verbatim}
Enter a year: 2020
Leap year
\end{verbatim}
\end{tcolorbox}

\vspace{0.5cm}

\subsection{if-else if-else}

当需要对更多的条件进行判断时，可以使用if-else if-else语句。\\

\mybox{字符}

\begin{lstlisting}[language=Java]
import java.util.Scanner;

public class Character {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.print("Enter a character: ");
		char c = scanner.next().charAt(0);

		if (c >= 'a' && c <= 'z') {
			System.out.println("Lowercase");
		} else if (c >= 'A' && c <= 'Z') {
			System.out.println("Uppercase");
		} else if (c >= '0' && c <= '9') {
			System.out.println("Digit");
		} else {
			System.out.println("Special character");
		}
		scanner.close();
	}
}	
\end{lstlisting}

\begin{tcolorbox}
	\mybox{运行结果}
	\begin{verbatim}
Enter a character: T
Uppercase
\end{verbatim}
\end{tcolorbox}

\newpage

\section{switch}

\subsection{switch}

switch结构用于根据一个整数值，选择对应的case执行。需要注意的是，当对应的case中的代码被执行完后，并不会像if语句一样跳出switch结构，而是会继续向后执行，直到遇到break。\\

\mybox{计算器}

\begin{lstlisting}[language=Java]
import java.util.Scanner;

public class Calculator {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.print("Enter an expression: ");
		int num1 = scanner.nextInt();
		char operator = scanner.next().charAt(0);
		int num2 = scanner.nextInt();
		scanner.close();

		switch (operator) {
		case '+':
			System.out.printf(
					"%d + %d = %d\n", num1, num2, num1 + num2
			);
			break;
		case '-':
			System.out.printf(
					"%d - %d = %d\n", num1, num2, num1 - num2
			);
			break;
		case '*':
			System.out.printf(
					"%d * %d = %d\n", num1, num2, num1 * num2
			);
			break;
		case '/':
			System.out.printf(
					"%d / %d = %d\n", num1, num2, num1 / num2
			);
			break;
		default:
			System.out.println("Error! Operator is not supported");
			break;
		}
	}
}
\end{lstlisting}

\begin{tcolorbox}
	\mybox{运行结果}
	\begin{verbatim}
Enter an expression: 5 * 8
5 * 8 = 40
\end{verbatim}
\end{tcolorbox}

\newpage